package com.yixun.qfbao.hander;

import com.yixun.qfbao.api.service.RoleResourcesService;
import com.yixun.qfbao.apidto.AuthorizeRoleReqDto;
import com.yixun.qfbao.apidto.AuthorizeRoleResDto;
import com.yixun.qfbao.dto.base.BaseRequestDTO;
import com.yixun.qfbao.dto.base.BaseResponseDTO;
import com.yixun.qfbao.hander.base.CommonAbstractHander;
import com.yixun.qfbao.model.PartnerInfo;
import com.yixun.qfbao.utils.LoginUtils;
import com.yixun.qfbao.vo.AuthorizeRoleReqVo;
import com.yixun.qfbao.xf.model.enums.ResultEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 角色授权
 *
 * @author guifang.weng
 * @create 2017-11-23 9:59
 **/
@Service
public class AuthorizeRoleHander extends CommonAbstractHander {
    private static Logger logger= LoggerFactory.getLogger(AuthorizeRoleHander.class);

    @Autowired
    private RoleResourcesService roleResourcesService;
    @Autowired
    private LoginUtils loginUtils;

    @Override
    public BaseResponseDTO doBusiness(BaseRequestDTO reqDto) {
        BaseResponseDTO<AuthorizeRoleResDto> res = new BaseResponseDTO<>();
        AuthorizeRoleResDto resDto = new AuthorizeRoleResDto();
        AuthorizeRoleReqVo reqVo = (AuthorizeRoleReqVo) reqDto;
        AuthorizeRoleReqDto authorizeRoleReqDto = reqVo.getParams();
        PartnerInfo pi = loginUtils.getUser(authorizeRoleReqDto.getUserId());
        if (pi == null) {
            res.setResult(ResultEnum.FAIL.getNo());
            res.setErrorMsg("未找到用户信息");
            return res;
        }
        authorizeRoleReqDto.setMerchantId(pi.getHigherid());
        try {
            resDto = roleResourcesService.authorizeRole(authorizeRoleReqDto);
        } catch (Exception e) {
            logger.error("AuthorizeRoleHander error: {}", e);
            resDto.setResult(ResultEnum.FAIL.getNo());
            resDto.setResultMsg("角色授权失败");
        }
        res.setResult(resDto.getResult());
        res.setResData(resDto);
        return res;
    }
}
